using System.Collections.Generic;
using Engine;
using Engine.Audio;

namespace RuthlessConquest {
    public static class AudioManager {
        public struct LastPlayedData {
            public double Time;

            public float Volume;
        }

        public static Dictionary<SoundBuffer, LastPlayedData> LastPlayedTime = new();

        public static void PlaySound(SoundBuffer soundBuffer, bool avoidRepetition, float volume = 1f, float pitch = 1f, float pan = 0f) {
            if (!(SettingsManager.SfxVolume > 0f)) {
                return;
            }
            float num = volume * global::Game.SettingsManager.SoundsVolume;
            if (avoidRepetition) {
                LastPlayedTime.TryGetValue(soundBuffer, out LastPlayedData value);
                if (Time.FrameStartTime > value.Time + 0.03
                    || num > value.Volume) {
                    LastPlayedTime[soundBuffer] = new LastPlayedData { Time = Time.FrameStartTime, Volume = num };
                    new Sound(soundBuffer, num, pitch, pan, false, true).Play();
                }
            }
            else {
                new Sound(soundBuffer, num, pitch, pan, false, true).Play();
            }
        }
    }
}